/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model.db;

import model.db.table.Table;

/**
 *
 * @author jc218934
 */
public class Insert
{
    public static void row(Table row)
    {

        String columnNamesString = getColumnNamesString(row);
        if (columnNamesString == null) return;
        String valuesString = getValuesString(row);
        if (valuesString == null) return;

        System.out.println("Inserting in DB "+row);

        if (!Execute.checkIfValueExist("select * from "+row.getTableNames()[0]+getWhereStr(row)))
        {
            Execute.execute("INSERT INTO "+row.getTableNames()[0]+" ("+columnNamesString+") VALUES ("+valuesString+")");
        }
        else
        {
            System.out.println("already in db");

        }
    }

    private static String getColumnNamesString(Table row)
    {
        String columnNamesStr = "";
        String [] columnNames = row.getColumnNames();
        int length = columnNames.length;
        if (length < 1) return null;
        columnNamesStr += columnNames[0];
        for (int i =1; i<length; ++i)
        {
            columnNamesStr += ", ";
            columnNamesStr += columnNames[i];
        }
        return columnNamesStr;
    }

    private static String getValuesString(Table row)
    {
        String valuesStr = "";
        Object [] columnValues = row.getColumnValues();
        Class [] columnTypes = row.getColumnTypes();
        int length = columnValues.length;
        if (length != columnTypes.length) return null;
        if (length < 1) return null;

        valuesStr += MySQLSyntax.correctSyntax(columnValues[0], columnTypes[0]);
        for (int i =1; i<length; ++i)
        {
            valuesStr += ", ";
            valuesStr += MySQLSyntax.correctSyntax(columnValues[i], columnTypes[i]);
        }
        return valuesStr;
    }

    public static void rowWithAutoincrementId(Table row)
    {
        System.out.println("Inserting in DB "+row);

        String columnNamesString = getColumnNamesStringAutoInc(row);
        if (columnNamesString == null) return;
        String valuesString = getValuesStringAutoInc(row);
        if (valuesString == null) return;

        if (!Execute.checkIfValueExist("select * from "+row.getTableNames()[0]+getWhereStrAutoInc(row)))
        {
            Execute.execute("INSERT INTO "+row.getTableNames()[0]+" ("+columnNamesString+") VALUES ("+valuesString+")");
        }
        else
        {
            System.out.println("already in db");

        }
        
        
    }

    private static String getColumnNamesStringAutoInc(Table row)
    {
        String columnNamesStr = "";
        String [] columnNames = row.getColumnNames();
        int length = columnNames.length;
        if (length < 1) return null;
        columnNamesStr += "rowid";
        for (int i =1; i<length; ++i)
        {
            columnNamesStr += ", ";
            columnNamesStr += columnNames[i];
        }
        return columnNamesStr;
    }

    private static String getValuesStringAutoInc(Table row)
    {
        String valuesStr = "";
        Object [] columnValues = row.getColumnValues();
        Class [] columnTypes = row.getColumnTypes();
        int length = columnValues.length;
        if (length != columnTypes.length) return null;
        if (length < 1) return null;

        valuesStr += "NULL";
        for (int i =1; i<length; ++i)
        {
            valuesStr += ", ";
            valuesStr += MySQLSyntax.correctSyntax(columnValues[i], columnTypes[i]);
        }
        return valuesStr;
    }

    private static String getWhereStr(Table row)
    {
        String [] columnNames = row.getColumnNames();

        Object [] columnValues = row.getColumnValues();
        Class [] columnTypes = row.getColumnTypes();
        int columnValuesLength = columnValues.length;
        int columnNamesLength = columnNames.length;
        if (columnValuesLength < 1) return null;
        if (columnValuesLength != columnTypes.length) return null;
        if (columnValuesLength != columnNamesLength) return null;

        String whereStr = " WHERE ";
        whereStr += columnNames[0]+ " = ";
        whereStr += MySQLSyntax.correctSyntax(columnValues[0], columnTypes[0]);
        for (int i =1; i<columnValuesLength; ++i)
        {
            whereStr += " AND ";
            whereStr += columnNames[i];
            whereStr += " = ";
            whereStr += MySQLSyntax.correctSyntax(columnValues[i], columnTypes[i]);
        }
        return whereStr;


    }

    private static String getWhereStrAutoInc(Table row)
    {
        String [] columnNames = row.getColumnNames();

        Object [] columnValues = row.getColumnValues();
        Class [] columnTypes = row.getColumnTypes();
        int columnValuesLength = columnValues.length;
        int columnNamesLength = columnNames.length;
        if (columnValuesLength < 1) return null;
        if (columnValuesLength != columnTypes.length) return null;
        if (columnValuesLength != columnNamesLength) return null;

        String whereStr = " WHERE ";
        whereStr += columnNames[1]+ " = ";
        whereStr += MySQLSyntax.correctSyntax(columnValues[1], columnTypes[1]);
        for (int i =2; i<columnValuesLength; ++i)
        {
            whereStr += " AND ";
            whereStr += columnNames[i];
            whereStr += " = ";
            whereStr += MySQLSyntax.correctSyntax(columnValues[i], columnTypes[i]);
        }
        return whereStr;
    }
}

